package com.jm.handler.exception;

import com.jm.domain.ResponseResult;
import com.jm.enums.AppHttpCodeEnum;
import com.jm.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 统一异常处理类
 */
//@ControllerAdvice
//@ResponseBody
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SystemException.class)
    public ResponseResult systemExceptionHandler(SystemException e) {
        //打印异常
        log.error("出现了异常：{}",e.getMsg());
        //从异常对象中获取异常信息并封装返沪
        return ResponseResult.errorResult(e.getCode(),e.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e) {
        //打印异常
        log.error("出现了异常：{}",e.getMessage());
        //从异常对象中获取异常信息并封装返沪
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),e.getMessage());
    }
}
